package org.jeecg.wms.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.basic.vo.MesOptionVO;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.wms.entity.WmsRuleCfg;
import org.jeecg.wms.service.IWmsRuleCfgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags="wms_rule_cfg")
@RestController
@RequestMapping("/wms/wmsRuleCfg")
@Slf4j
public class WmsRuleCfgController extends JeecgController<WmsRuleCfg, IWmsRuleCfgService> {

    @Autowired
    private IWmsRuleCfgService wmsRuleCfgService;
    @Autowired
    private CommonAPI commonAPI;

    /**
     * 分页列表查询
     *
     * @param wmsRuleCfg
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "qis_reinspect_cfg-分页列表查询")
    @ApiOperation(value="qis_rule_cfg-分页列表查询", notes="qis_rule_cfg-分页列表查询")
    @GetMapping(value = "/list")
    @DS("WMS")
    public Result<IPage<WmsRuleCfg>> queryPageList(WmsRuleCfg wmsRuleCfg,
                                                        @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                        @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<WmsRuleCfg> queryWrapper = QueryGenerator.initQueryWrapper(wmsRuleCfg, req.getParameterMap());
        if (StringUtils.isNotBlank(wmsRuleCfg.getGroupnam())) {
            queryWrapper.or(qw -> qw.like("GROUPNAM",wmsRuleCfg.getGroupnam())// 完全包含
            );
        }
        queryWrapper.orderByDesc("REC_ID");
        Page<WmsRuleCfg> page = new Page<WmsRuleCfg>(pageNo, pageSize);
        IPage<WmsRuleCfg> pageList = wmsRuleCfgService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog(value = "qis_rule_cfg-校验")
    @ApiOperation(value="qis_rule_cfg-校验", notes="qis_rule_cfg-校验")
    @PostMapping(value = "/validateRule")
    @DS("WMS")
    public Result<String> validateRule(@RequestBody WmsRuleCfg wmsRuleCfg) {
        // 参数合法性校验
        if (wmsRuleCfg.getUpLimit() == null || wmsRuleCfg.getLowLimit() == null) {
            return Result.error("上限值和下限值不能为空");
        }
        if (wmsRuleCfg.getUpLimit().compareTo(wmsRuleCfg.getLowLimit()) <= 0) {
            return Result.error("上限值必须大于下限值");
        }
        if (wmsRuleCfg.getUpLimit().compareTo(wmsRuleCfg.getLowLimit()) == 0) {
            return Result.error("上限值和下限值不能相同");
        }
        if (wmsRuleCfgService.validateRuleRange(wmsRuleCfg)) {
            return Result.error("当前分组的上下限存在范围重叠");
        } else {
            return Result.OK("校验通过");
        }
    }

    /**
     *   添加
     *
     * @param wmsRuleCfg
     * @return
     */
    @AutoLog(value = "qis_rule_cfg-添加")
    @ApiOperation(value="qis_rule_cfg-添加", notes="qis_rule_cfg-添加")
    @PostMapping(value = "/add")
    @DS("WMS")
    public Result<String> add(@RequestBody WmsRuleCfg wmsRuleCfg) {
        Integer counterValue = commonAPI.getCounterValue("recid.qis_rule_cfg","0");
        wmsRuleCfg.setRecId(counterValue);
        log.info("WmsRuleCfgController中的SecurityUtils.getSubject信息：", SecurityUtils.getSubject().isAuthenticated());
        wmsRuleCfgService.save(wmsRuleCfg);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param wmsRuleCfg
     * @return
     */
    @AutoLog(value = "qis_rule_cfg-编辑")
    @ApiOperation(value="qis_rule_cfg-编辑", notes="qis_rule_cfg-编辑")
    //@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_cfg:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    @DS("WMS")
    public Result<String> edit(@RequestBody WmsRuleCfg wmsRuleCfg) {
        wmsRuleCfgService.updateById(wmsRuleCfg);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过选中对象删除
     *
     * @param wmsRuleCfg
     * @return
     */
    @AutoLog(value = "qis_rule_cfg-通过id删除")
    @ApiOperation(value="qis_rule_cfg-通过id删除", notes="qis_rule_cfg-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_cfg:delete")
    @RequestMapping(value = "/delete")
    @DS("WMS")
    public Result<String> delete(@RequestBody WmsRuleCfg wmsRuleCfg) {
        wmsRuleCfgService.removeById(wmsRuleCfg);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param list
     * @return
     */
    @AutoLog(value = "qis_rule_cfg-批量删除")
    @ApiOperation(value="qis_rule_cfg-批量删除", notes="qis_rule_cfg-批量删除")
    //@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_cfg:deleteBatch")
    @RequestMapping(value = "/deleteBatch")
    @DS("WMS")
    public Result<String> deleteBatch(@RequestBody List<WmsRuleCfg> list) {
        list.forEach(item->{
            wmsRuleCfgService.removeById(item);
        });
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "qis_reinspect_cfg-通过id查询")
    @ApiOperation(value="qis_rule_cfg-通过id查询", notes="qis_rule_cfg-通过id查询")
    @GetMapping(value = "/queryById")
    @DS("WMS")
    public Result<WmsRuleCfg> queryById(@RequestParam(name="id",required=true) String id) {
        WmsRuleCfg wmsRuleCfg = wmsRuleCfgService.getById(id);
        if(wmsRuleCfg==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsRuleCfg);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsRuleCfg
     */
    //@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_cfg:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("WMS")
    public ModelAndView exportXls(HttpServletRequest request, WmsRuleCfg wmsRuleCfg) {
        return super.exportXls(request, wmsRuleCfg, WmsRuleCfg.class, "qis_rule_cfg");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("qis_reinspect_cfg:importExcel")
    @DS("WMS")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WmsRuleCfg.class);
    }


    @ApiOperation(value = "查询抽样配置信息列表", notes = "查询抽样配置信息列表")
    @GetMapping(value = "/ruleSelect")
    @DS("WMS")
    public Result<List<MesOptionVO>> ruleSelect() {
        List<MesOptionVO> ruleSelect = wmsRuleCfgService.getRuleCfgSelect();
        if (ruleSelect.isEmpty()) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(ruleSelect);
    }

}
